/**
 * @author Eric Jahn
 * Course: Object Oriented Programming
 * Final Project
 * Due Date: August 3, 2012
 */
package oop.chat.user;

import java.io.Serializable;
import oop.chat.data.ConverseDB;

/**
 * main class to manage a user account
 * @author Eric Jahn
 */
public class UserAccount implements Serializable
{	
    //user type 	
    private UserType type;
    // password
    private String password;
    //contact information 
    private Contact contactInfo;
    //collection of contacts 	
    private ContactList[] contactLists;
	//login value
	private boolean isLoggedIn;


    /**
     * user account constructor I
     * @param	type	UserType
     */
    public UserAccount(UserType type)
    {
        this(type, null, null);
    }

    /**
     * user account constructor II
     * @param	type            UserType
     * @param   password        String
     * @param	contactInfo	Contact
     */
    public UserAccount(UserType type, Contact contactInfo)
    {
        this(type, contactInfo, null);
    }

    /**
     * user account constructor II
     *
     * @param	type            UserType
     * @param   password        String
     * @param	contactInfo	Contact
     * @param	contacts	ContactList
     */
    public UserAccount(UserType type, Contact contactInfo, ContactList[] contacts)
    {
		this.isLoggedIn = false;
		this.type = type;
        this.contactInfo = contactInfo;
		//read password from file
        this.password = ConverseDB.getPasswordFromFile(this.contactInfo.getPID());
		if(contacts != null)
		{			
			this.contactLists = contacts;
		}
		else
		{
			this.contactLists = new ContactList[Category.values().length];
			//initialize all possible contact lists
			int index;
			for(Category category: Category.values())
			{
				index = category.getIndex();
				if(index == Category.COMMON_LIST.getIndex())
				{
					this.contactLists[index] = new ContactList();
				}
				else
				{
					this.contactLists[index] = new SpecialList(category);
				}
			}
		}
    }

    /**
     * gets the contact list of the user
     * @return	contactList	ContactList
     */
    public final ContactList[] getContactLists()
    {
        return this.contactLists;
    }
	
	/**
	 * gets a special contact list by category
 	 * @param	category		Category
	 * @return	ContactList
	 */
	public final ContactList getContactListByCategory(Category category)
	{
		return this.contactLists[category.getIndex()];
	}

    /**
     * sets the contact information of the user
     * @param	contactInfo	Contact
     */
    public void setContactInfo(Contact contactInfo)
    {
        this.contactInfo = contactInfo;
    }

    /**
     * gets the contact information of the user
     * @return	contactInfo	Contact
     */
    public final Contact getContactInfo()
    {
        return this.contactInfo;
    }
	
	/**
	 * adds a contact to the default list
	 * @param	contact		Contact 
	 */
    public void addContact(Contact contact)
    {
		this.addContact(contact, Category.COMMON_LIST);
    }
	
	/**
	 * adds a contact to the special list
	 * @param	contact		Contact
	 * @param	category	Category
	 */
	public void addContact(Contact contact, Category category)
	{
		this.contactLists[category.getIndex()].addContact(contact);
	}
	
	/**
	 * gets the user type
	 * @return	tpye
	 */
	public final UserType getUserType()
	{
		return this.type;
	}

	/**
	 * gets the password of the user
	 * @return	password		String
	 */
    public final String getPassword()
    {
        return this.password;
    }

	/**
	 * sets a password
	 * @param	password		String
	 */
    public void setPassword(String password)
    {
        this.password = ConverseDB.md5Password(password);
    }
	
	/**
	 * sets the login status of the user
	 * @param	login		boolean
	 */
	public void setLogin(boolean login)
	{
		this.isLoggedIn = login;
	}
	
	/**
	 * gets the login status of the user
	 * @return	login		boolean
	 */
	public final boolean isLoggedIn()
	{
		return this.isLoggedIn;
	}
    
    @Override
    public String toString()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append(this.type);
        buffer.append(",");
        buffer.append("******");  // this is the password
        buffer.append(",");
        buffer.append(this.contactInfo);
        buffer.append(",");
        buffer.append(this.contactLists);
        /*buffer.append(", SpecialLists:  ");
        
        Set entries = specialLists.entrySet();
        Iterator iter = entries.iterator();
        while (iter.hasNext())
        {
            Map.Entry entry = (Map.Entry) iter.next();
            buffer.append("Category:  ");
            buffer.append(entry.getKey());
            buffer.append(" ");
            buffer.append(entry.getValue());
            buffer.append("\n");
        }*/
        
        return buffer.toString();
    }
}
